class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) {
        int n = s.size();
        sort(dictionary.begin(),dictionary.end(),[](auto& s1,auto& s2){
            if(s1.size() == s2.size()){
                return s1 < s2;
            }
            return s1.size() > s2.size();
        });
        
        for(string& rec : dictionary){
            int m = rec.size();
            int i = 0,j = 0;
            while(i < n && j < m){
                if(s[i] == rec[j]){
                    i++; j++;
                }else{
                    i++;
                }
            }
            if(j == m) return rec;
        }
        
        return "";
    }
};
